package utils;

import com.alibaba.druid.pool.DruidDataSource;

import java.sql.*;
import java.util.List;
import java.util.ResourceBundle;

public class DbUtils {
    //1.定义变量
    private Connection connection;
    protected PreparedStatement preparedStatement;
    private ResultSet resultSet;
    private int count;
    //德鲁伊
    private static DruidDataSource dataSource = new DruidDataSource();
    private static String userName;
    private static String userPass;
    private static String url;
    private static String driver;
    //2.加载驱动
    static {
/*        try {
            InputStream is = DBUtils.class.getClassLoader().getResourceAsStream("mysql.properties");
            Properties properties = new Properties();
            properties.load(is);
            userName = properties.getProperty("uname");
            userPass = properties.getProperty("upass");
            url = properties.getProperty("url");
            Class.forName(properties.getProperty("driver"));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        //DBCP
/*        ResourceBundle bundle = ResourceBundle.getBundle("mysql");
        driver = bundle.getString("driver");
        userName = bundle.getString("uname");
        userPass = bundle.getString("upass");
        url = bundle.getString("url");
        basicDataSource = new BasicDataSource();
        basicDataSource.setUsername(userName);
        basicDataSource.setPassword(userPass);
        basicDataSource.setUrl(url);
        basicDataSource.setDriverClassName(driver);*/
        //德鲁伊
        ResourceBundle bundle = ResourceBundle.getBundle("mysql");
        driver = bundle.getString("driver");
        userName = bundle.getString("uname");
        userPass = bundle.getString("upass");
        url = bundle.getString("url");

        dataSource.setUsername(userName);
        dataSource.setPassword(userPass);
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driver);

    }
    //3.获得连接
    protected Connection getConnection(){
        try {
            //DBCP
//            connection = basicDataSource.getConnection();
            //c3p0
//            connection = comboPooledDataSource.getConnection();
            //德鲁伊
            connection = dataSource.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }
    //4.得到预状态通道
    protected PreparedStatement getPreparedStatement(String sql){
        try {
            preparedStatement = getConnection().prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return preparedStatement;
    }
    //5.绑定参数 list保存的是给占位符所赋的值
    protected void param(List list){
        if (list!=null&&list.size()>0){
            for (int i = 0; i < list.size(); i++) {
                try {
                    preparedStatement.setObject(i+1,list.get(i));
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
    //6.执行操作（增删改）
    protected int update(String sql,List list){
        getPreparedStatement(sql);
        param(list);
        try {
            count = preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }
    //7.查询
    protected ResultSet query(String sql,List list){
        getPreparedStatement(sql);
        param(list);
        try {
            resultSet = preparedStatement.executeQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return resultSet;
    }
    //8.关闭资源
    protected void closeAll(){
        try {
            if (connection != null) connection.close();
            if (preparedStatement != null) preparedStatement.close();
            if (resultSet!= null) preparedStatement.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
